package com.yuven.baselib.component.viewmodel;


import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;

import com.yuven.baselib.component.bean.DialogBean;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 
 *
 * @author :        Yuven
 * @date :          2022/7/6 6:55 下午
 * @desc :
 */
public class BaseViewModel extends ViewModel {
    /**
     * 管理RxJava请求
     */
    private CompositeDisposable compositeDisposable = new CompositeDisposable();


    /**
     * 业务Activity／Fragment弹框管理
     */
    protected DialogLiveData<DialogBean> showDialog = new DialogLiveData();
    protected MutableLiveData<String> toastLiveData = new MutableLiveData();

    public void observeDialog(LifecycleOwner owner, Observer<DialogBean> observer) {
        showDialog.observe(owner, observer);
    }

    public void observeToast(LifecycleOwner owner, Observer<String> observer) {
        toastLiveData.observe(owner, observer);
    }

    /**
     * 添加 rxJava 发出的请求
     */
    protected void addDisposable(Disposable disposable) {
        if (disposable != null) {
            compositeDisposable.add(disposable);
        }
    }


    /**
     * ViewModel销毁同时也取消请求
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        compositeDisposable.clear();
    }

    public CompositeDisposable getCompositeDisposable() {
        return compositeDisposable;
    }

    public static class EmptyVM extends BaseViewModel {

    }
}
